package com.android.aidltest;

import com.google.android.apps.gddquiz.IQuizService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TestActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	private String mGetCodeStr = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnBind = (Button) findViewById(R.id.btnBind);
        btnBind.setOnClickListener(this);
        
        Button btnUnbind = (Button) findViewById(R.id.btnUnbind);
        btnBind.setOnClickListener(this);
        
        Button btnCall = (Button) findViewById(R.id.btnCall);
        btnCall.setOnClickListener(this);
    }
    
    private ServiceConnection conn = new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			IQuizService quizService = IQuizService.Stub.asInterface(service);
			try {
				//Log.v("ServiceConnectionTest", "aa");
				mGetCodeStr = quizService.getCode();
			} catch (RemoteException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {

		}
    	
    };

	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		switch(v.getId()){
		case R.id.btnBind:
			Intent intent = new Intent(IQuizService.class.getName());
			bindService(intent, conn, BIND_AUTO_CREATE);
			Toast.makeText(this, "bind", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btnUnbind:
			unbindService(conn);
			Toast.makeText(this, "unbind", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btnCall:
			TextView tvResult = (TextView) findViewById(R.id.tvResult);
			tvResult.setText(mGetCodeStr);
			Toast.makeText(this, "getCode", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}
}